package com.wm.javamall.cart.controller;

import com.alibaba.fastjson.TypeReference;

import com.wm.common.to.CategoryVo;
import com.wm.common.utils.R;
import com.wm.javamall.cart.service.CartService;
import com.wm.javamall.cart.vo.Cart;
import com.wm.javamall.cart.vo.CartItem;
import com.wm.javamall.product.controller.CategoryController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Author: sshdg
 * @Date: 2021/2/23 9:42
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @Autowired
    CategoryController categoryController;


    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://localhost:11000/cart/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num")Integer num){
        cartService.changeItemCount(skuId,num);
        return "redirect:http://localhost:11000/cart/cart.html";
    }


    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check")Integer check){

        cartService.checkItem(skuId,check);
        return "redirect:http://localhost:11000/cart/cart.html";
    }

    /**
     * 浏览器有一个cookie, user-key:标识用户身份，一个月后过期
     * 如果第一次使用jd的购物车功能，都会给一个临时的用户身份
     * 浏览器以后保存，每次访问都会带上这个cookie
     *
     * 登录：session有
     * 没登录：按照cookie里面带来的user-key来做
     * 第一次：如果没有临时用户，帮忙创建一个临时用户
     * @return
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        R cat = categoryController.getCat();
        List<CategoryVo> category = cat.getData("category", new TypeReference<List<CategoryVo>>() {
        });
        model.addAttribute("category",category);
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartlist";
    }



    /**
     * 添加商品到购物车
     * RedirectAttributes
     *     ra.addFlashAttribute();将数据放在session里面可以在页面取出，但是只能取一次
     *     ra.addAttribute("skuId",skuId); 将数据放在请求路径后
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num,
                            RedirectAttributes ra) throws ExecutionException, InterruptedException {

        cartService.addToCart(skuId,num);
        //RedirectAttributes，重定向时会将参数放到地址后
        ra.addAttribute("skuId",skuId);
        ra.addAttribute("num",num);
        return "redirect:http://localhost:11000/cart/addToCartSuccess.html";
    }

    /**
     * 跳转到成功页：可以理解为刷新页面时把num过滤掉，重新查询数据展示
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,
                                       @RequestParam("num") Integer num,
                                       Model model){
        R cat = categoryController.getCat();
        List<CategoryVo> category = cat.getData("category", new TypeReference<List<CategoryVo>>() {
        });
        model.addAttribute("category",category);
        //重向到成功页面，再次查询购物车数据即可
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item",cartItem);
        model.addAttribute("num",num);

        return "cartsuccess";
    }
}
